package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-05 01:03
 **/
public class LeetCode1576 {
    public String modifyString(String s) {
        char[] chars = s.toCharArray();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            if (chars[i] == '?') {
                char c = 'a';
                while ((i > 0 && chars[i - 1] == c) || (i < n - 1 && chars[i + 1] == c)) {
                    c = (char)(c + 1);
                }
                chars[i] = c;
            }
        }
        return new String(chars);
    }


    /*public String modifyString(String s) {
        int n = s.length();
        if (n == 1) {
            return "?".equals(s) ? "a" : s;
        }

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (c == '?') {
                if (i == 0) {
                    if (s.charAt(1) == '?') {
                         builder.append('a');
                    } else {
                        builder.append(s.charAt(1) == 'a' ? 'b' : 'a');
                    }
                } else if (i == n - 1) {
                    builder.append(builder.charAt(n - 2) == 'a' ? 'b' : 'a');
                } else {
                    char init = 'a';
                    while (init == builder.charAt(i - 1) || init == s.charAt(i + 1)) {
                        init = (char) (init + 1);
                    }
                    builder.append(init);

                }
            } else {
                builder.append(c);
            }
        }

        return builder.toString();
    }
*/
}
